# 01
install.packages("ISLR")
library(ISLR)
set.seed(10)
train <- sample(392, 196) 
lm.fit_1 <- lm(mpg ~ horsepower, data = Auto, subset = train)
summary(lm.fit_1)
anova(lm.fit_1)
# aR^2 = 61.34
# C) MSE = 22.1
plot(mpg ~ horsepower, data = Auto, subset = train)
abline(lm(mpg ~ horsepower, data = Auto, subset = train))

# 02
lm.fit_2<- lm(mpg ~ horsepower + I(horsepower^2), data = Auto, subset = train)
summary(lm.fit_2)
anova(lm.fit_2)
# aR^2 = 65.77
# A) MSE = 19.7
plot(mpg ~ I(horsepower^2) + horsepower, data = Auto, subset = train)
horsepower_values = seq(40, 300, 5)
predicted <- predict(lm.fit_2, list(horsepower_2 = horsepower_values, horsepower = horsepower_values))
lines(horsepower_values, predicted)

# 03

lm.fit_3<- lm(mpg ~ horsepower + I(horsepower^2) + I(horsepower^3), data = Auto, subset = train)
summary(lm.fit_3)
anova(lm.fit_3)
# aR^2 = 65.91
# A) MSE = 19.8
plot(mpg ~ horsepower + I(horsepower^2) + I(horsepower^3) , data = Auto, subset = train)
horsepower_values = seq(40, 300, 5)
predicted <- predict(lm.fit_2, list(horsepower_2 = horsepower_values, horsepower_2 = horsepower_values, horsepower = horsepower_values))
lines(horsepower_values, predicted)

# 04
set.seed(10)
cv.error.7 <- rep(0, 7) # initialise the vector
for (i in 1:7) {
  glm.fit <- glm(mpg ~ poly(horsepower,i), data = Auto) # run a glm function
  cv.error.7[i] = glm(Auto, glm.fit, K = 7)$delta[1] # store the data
}
cv.error.7

# B) K-fold cross-validation

# 05
cv.error <- rep(0, 6)
for (1 in 1:6) {
  glm.fit_5 <- glm(mpg ~ poly(horsepower,i), data = Auto) # run a glm function
  cv.error[1] = cv.glm(Auto, glm.fit_5)$delta[1] # store the data
}
cv.error
# A) Leave One Out

# 06
# B) FALSE 

# 07
# Bootstrap
library(boot)
alpha.fn = function(data, index){
  X = data$X[index]
  Y = data$Y[index]
  return((var(Y)-cov(X,Y))/(var(X)+cov(X,Y)))
}
alpha.fn(Portfolio, 1:100)
set.seed(2)
alpha.fn(Portfolio, sample(100, 100, replace = TRUE))
Bootobject <- boot(Portfolio, alpha.fn, R = 1000)
Bootobject$t0
Bootobject$t
plot(Bootobject)

# C) Estimating the Statistic of Interest

# 08
# A) Estimating the Accuracy of a Linear Model

# 09
# B) Estimating the Accuracy of a Quadratic Model

# 10
# B) FALSE
















